package com.gxa.modules.pc.hospital.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxa.modules.model.dto.PackageDto;
import com.gxa.modules.model.dto.PackageForChooseDto;
import com.gxa.modules.model.entity.hospital.Health;
import com.gxa.modules.model.entity.hospital.Items;
import com.gxa.modules.model.entity.hospital.Package;
import com.gxa.modules.model.entity.hospital.PackageItems;
import com.gxa.modules.model.vo.PackageVo;
import com.gxa.modules.pc.hospital.mapper.*;
import com.gxa.modules.pc.hospital.service.PackageItemsService;
import com.gxa.modules.pc.hospital.service.PackageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.xml.crypto.Data;
import java.util.*;

/**
 * <p>
 * 套餐信息 服务实现类
 * </p>
 *
 * @author gxa
 * @since 2022-11-12 05:04:59
 */
@Service
public class PackageServiceImpl extends ServiceImpl<PackageMapper, Package> implements PackageService {

    @Autowired
    private PackageMapper packageMapper;

    //院区
    @Autowired
    private HospitalParkMapper hospitalParkMapper;

    //体检项目
    @Autowired
    private ItemsMapper itemsMapper;

    //套餐类型
    @Autowired
    private PackageTypeMapper packageTypeMapper;


    @Autowired
    private PackageItemsMapper packageItemsMapper;

    @Autowired
    private PackageItemsService packageItemsService;

    @Override
    public Map<String,Object> queryAll(PackageVo packageVo) {
        Map map = new HashMap();
        Integer page = (packageVo.getPage()-1)*packageVo.getSize();
        packageVo.setPage(page);
        List<PackageDto> packageDtos = this.packageMapper.queryAll(packageVo);
        Integer count = this.packageMapper.count();
        map.put("lists",packageDtos);
        map.put("count",count);
        return map;
    }

    @Override
    public void delById(List<Long> list) {
        QueryWrapper<Package> wrapper = new QueryWrapper<>();
        wrapper.in("id",list);
        this.packageMapper.delete(wrapper);
    }

    @Override
    public void updateStateById(Integer id, Integer state) {
        this.packageMapper.updateStateById(id, state);
    }

    @Override
    public Package queryById(Integer id) {
        Package aPackage = this.packageMapper.queryById(id);
        return  aPackage;
    }

    @Override
    public String delByIds(List<Long> ids) {
        QueryWrapper<Package> wrapper = new QueryWrapper<>();
        wrapper.in("id",ids);
        packageMapper.delete(wrapper);

        return "删除成功";
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(Package hospitalPackage) {
        packageMapper.insert(hospitalPackage);
        Integer id = hospitalPackage.getId();
        List<PackageItems> lists = new ArrayList<>();
        PackageItems packageItems = new PackageItems();
        packageItems.setPackageId(id);
        List<Integer> packageLists = hospitalPackage.getLists();
        for (Integer Itemsid: packageLists ) {
            packageItems.setItemsId(Itemsid);
            lists.add(packageItems);
        }
        this.packageItemsMapper.add(lists);
    }

    @Override
    public List<Items> queryForChoose() {
        QueryWrapper<Items> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "name", "price");
        List<Items> itemsList = itemsMapper.selectList(queryWrapper);
        return itemsList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Package apackage) {
        List<Integer> newlists = apackage.getLists();
        packageMapper.update(apackage);
        List<Integer> integers = packageMapper.queryAllByPackageId(apackage.getId());
        if (newlists.size() == integers.size()) {
            this.packageMapper.update(apackage);
        } else {
            for (int i : newlists) {
                for (int j : integers) {
                    if (i == j) {
                        newlists.remove(i);
                        integers.remove(j);
                    }
                }
            }
            if (integers.size() == 0) {
                List<PackageItems> itemlists = new ArrayList<>();
                PackageItems packageItems = new PackageItems();
                packageItems.setPackageId(apackage.getId());
                for (Integer Itemsid : newlists) {
                    packageItems.setItemsId(Itemsid);
                    itemlists.add(packageItems);
                }
                this.packageItemsMapper.add(itemlists);
            } else {
                this.packageItemsService.delete(integers);
            }
        }
    }
}
